home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-src / vcpp / include.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  3KB  |  135 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "cpp.h"
  4.  
  5. Includelist    includelist[NINCLUDE];
  6.  
  7. extern char    *objname;
  8.  
  9. void
  10. doinclude(Tokenrow *trp)
  11. {
  12.     char fname[256], iname[256];
  13.     Includelist *ip;
  14.     int angled, len, fd, i;
  15.  
  16.     trp->tp += 1;
  17.     if (trp->tp>=trp->lp)
  18.         goto syntax;
  19.     if (trp->tp->type!=STRING && trp->tp->type!=LT) {
  20.         len = trp->tp - trp->bp;
  21.         expandrow(trp, "<include>");
  22.         trp->tp = trp->bp+len;
  23.     }
  24.     if (trp->tp->type==STRING) {
  25.         len = trp->tp->len-2;
  26.         if (len > sizeof(fname) - 1)
  27.             len = sizeof(fname) - 1;
  28.         strncpy(fname, (char*)trp->tp->t+1, len);
  29.         angled = 0;
  30.     } else {
  31.         len = 0;
  32.         trp->tp++;
  33.         while (trp->tp->type!=GT) {
  34.             if (trp->tp>trp->lp || len+trp->tp->len+2 >= sizeof(fname))
  35.                 goto syntax;
  36.             strncpy(fname+len, (char*)trp->tp->t, trp->tp->len);
  37.             len += trp->tp->len;
  38.             trp->tp++;
  39.         }
  40.         angled = 1;
  41.     }
  42.     trp->tp += 2;
  43.     if (trp->tp < trp->lp || len==0)
  44.         goto syntax;
  45.     fname[len] = '\0';
  46.     if (fname[0]=='/') {
  47.         fd = open(fname, 0);
  48.         strcpy(iname, fname);
  49.     } else for (fd = -1,i=NINCLUDE-1; i>=0; i--) {
  50.         ip = &includelist[i];
  51.         if (ip->file==NULL || ip->deleted || (angled && ip->always==0))
  52.             continue;
  53.         if (strlen(fname)+strlen(ip->file)+2 > sizeof(iname))
  54.             continue;
  55.         strcpy(iname, ip->file);
  56. #ifndef AMIGA
  57.         strcat(iname, "/");
  58. #else
  59.         if (strcmp(iname, ".") == 0) {
  60.           /* "./foo" doesn't work on the amiga--use "foo" instead" */
  61.           iname[0] = 0;
  62.         }else{
  63.           if (iname[strlen(iname)-1] != ':' && iname[strlen(iname)-1] != '/') {
  64.            /* do not append "/" to volume names and paths ending in
  65.               "/", which is the Amiga's equivalent of ".." */
  66.             strcat(iname, "/");
  67.           }
  68.         }
  69. #endif
  70.         strcat(iname, fname);
  71.         if ((fd = open(iname, 0)) >= 0)
  72.             break;
  73.     }
  74.     if ( Mflag>1 || !angled&&Mflag==1 ) {
  75.         write(1,objname,strlen(objname));
  76.         write(1,iname,strlen(iname));
  77.         write(1,"\n",1);
  78.     }
  79.     if (fd >= 0) {
  80.         if (++incdepth > 10)
  81.             error(FATAL, "#include too deeply nested");
  82.         setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL);
  83.         genline();
  84.     } else {
  85.         trp->tp = trp->bp+2;
  86.         error(ERROR, "Could not find include file %r", trp);
  87.     }
  88.     return;
  89. syntax:
  90.     error(ERROR, "Syntax error in #include");
  91.     return;
  92. }
  93.  
  94. /*
  95.  * Generate a line directive for cursource
  96.  */
  97. void
  98. genline(void)
  99. {
  100.     static Token ta = { UNCLASS };
  101.     static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
  102.     uchar *p;
  103.  
  104.     ta.t = p = (uchar*)outp;
  105.     strcpy((char*)p, "#line ");
  106.     p += sizeof("#line ")-1;
  107.     p = (uchar*)outnum((char*)p, cursource->line);
  108.     *p++ = ' '; *p++ = '"';
  109.     if (cursource->filename[0]!='/' && wd[0]) {
  110.         strcpy((char*)p, wd);
  111.         p += strlen(wd);
  112.         *p++ = '/';
  113.     }
  114.     strcpy((char*)p, cursource->filename);
  115.     p += strlen((char*)p);
  116.     *p++ = '"'; *p++ = '\n';
  117.     ta.len = (char*)p-outp;
  118.     outp = (char*)p;
  119.     tr.tp = tr.bp;
  120.     puttokens(&tr);
  121. }
  122.  
  123. void
  124. setobjname(char *f)
  125. {
  126.     int n = strlen(f);
  127.     objname = (char*)domalloc(n+5);
  128.     strcpy(objname,f);
  129.     if(objname[n-2]=='.'){
  130.         strcpy(objname+n-1,"$O: ");
  131.     }else{
  132.         strcpy(objname+n,"$O: ");
  133.     }
  134. }
  135.